// pages/me/me.js
import {request, loadImageUrl} from "../../request/request.js";
import Notify from '../../miniprogram_npm/@vant/weapp/notify/notify';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {
      userId: 0,
      username: '',
      nickname: '',
      position: false,
      avatar: ''
    },
    totalArticle: 0,
    totalCommentAmount: 0,
    totalHits: 0,
    totalUser: 0,
    //密码修改的显示
    show: false,
    //修改密码
    oldPassword: '',
    password: '',
    confirmPassword: ''
  },
  toLogin() {
    wx.navigateTo({
      url: '/pages/login/login'
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //回去内存中的数据
    const userInfo = wx.getStorageSync('userInfo');
    console.log(userInfo);
    if(userInfo !== null && userInfo !== '') {
      console.log("测试")
      this.setData({
        userInfo: userInfo
      })
    }
    this.getTotalStatistics();
    
  },
  //获取全部的统计数据
  async getTotalStatistics() {
    const res = await request({
      url: '/statistics/getTotalStatistics'
    })
    console.log(res);
    if(res.code === 2000) {
      this.setData({
        totalArticle: res.data.statisticVo.totalArticle,
        totalCommentAmount: res.data.statisticVo.totalComment,
        totalHits: res.data.statisticVo.totalHits,
        totalUser: res.data.statisticVo.totalUser,
      })
    }
  },
  show() {
    this.setData({ show: true });
  },
  onClose() {
    this.setData({
      oldPassword: '',
      password: '',
      confirmPassword: ''
    })
    this.setData({ show: false });
  },
  async onConfirm() {
    const oldPassword = this.data.oldPassword;
    const password = this.data.password;
    const confirmPassword = this.data.confirmPassword;
    if(oldPassword === '' || password === '' || confirmPassword === '') {
      Notify({ type: 'danger', message: '不能为空！' });
      return;
    }
    if(password.length < 6 || confirmPassword.length <6 || oldPassword.length < 6) {
      Notify({ type: 'danger', message: '密码长度不能少于6位数！' });
      return;
    }
    if(password === oldPassword) {
      Notify({ type: 'danger', message: '新密码不能和原始密码一致！' });
      return;
    }
    if(password !== confirmPassword) {
      Notify({ type: 'danger', message: '两次密码不一致！' });
      return;
    }
    const res = await request({
      url: '/user/changePassword',
      method: 'post', 
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        oldPassword,
        password,
        'username': this.data.userInfo.username
      }
    })
    console.log(res);
    if(res.code === 4000) {
      Notify({ type: 'danger', message: '原始密码错误！' });
      return;
    }
    if(res.code === 2000) {
      this.logOut();
    }
  },
  //退出登录
  async logOut() {
    const res = await request({
      url: '/user/logOut'
    })
    if(res.code === 2000) {
      this.setData({ show: false });
      wx.removeStorageSync('userInfo');
      wx.removeStorageSync('token');
      this.setData({
        'userInfo.userId': 0,
        'userInfo.username': '',
        'userInfo.nickname': '',
        'userInfo.position': false,
        'userInfo.avatar': ''
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  //上传头像
  afterRead(event) {
    const { file } = event.detail;
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    wx.uploadFile({
      url: loadImageUrl, // 仅为示例，非真实的接口地址
      filePath: file.path,
      name: 'file',
      formData: { username: this.data.userInfo.username },
      success: (res) => {
        const jsonObj = JSON.parse(res.data);
        console.log(jsonObj);
        this.setData({
          'userInfo.avatar': jsonObj.data.url
        })
        wx.setStorageSync('userInfo', this.data.userInfo)

      },
    });
  },
  //密码修改输入框赋值
  inputOldPassword(event) {
    this.setData({
      oldPassword: event.detail
    })
  },
  inputPassword(event) {
    this.setData({
      password: event.detail
    })
  },
  inputComfirmPassword(event) {
    this.setData({
      confirmPassword: event.detail
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})